package codename.carrot.bomberman.controllers;

import codename.carrot.bomberman.game.listener.ObstacleListener;
import codename.carrot.bomberman.models.AbstractObstacle;
import codename.carrot.bomberman.models.ItemContainer;
import codename.carrot.bomberman.models.Level;

/**
 * ItemContainerController is responsible for handling itemcontainer-explosions
 * ( based on events coming from the server
 * 
 * @author Group A4
 * 
 */
public class ItemContainerController implements ObstacleListener {

	/**
	 * The current level.
	 */
	private Level level;

	/**
	 * Constructor.
	 * 
	 * @param level The current level.
	 */
	public ItemContainerController(Level level) {
		this.level = level;
	}

	/**
	 * Sets the level which this controller will work with.
	 * 
	 * @param level
	 */
	public void setLevel(Level level) {
		this.level = level;
	}

	@Override
	public void onObstacleExploded(AbstractObstacle obstacle) {
		if (level == null)
			return;
		// Remove from level
		level.removeObstacle(obstacle);
		if (obstacle instanceof ItemContainer) {
			// Log.d("InsoItem",obstacle.toString());
			ItemContainer container = (ItemContainer) obstacle;
			if (container.hasItem()) {
				level.addItem(container.getItem());
			}
			level.removeObstacle(obstacle);
		}
	}

	/**
	 * Notification that the <code>obstacle</code> is due to be removed
	 */
	@Override
	public void onRemoveObstacle(AbstractObstacle obstacle) {
		level.removeObstacle(obstacle);
	}

	/**
	 * Notification that the <code>obstacle</code> has changed visibility
	 */
	@Override
	public void onObstacleVisibilityChanged(AbstractObstacle obstacle,
			boolean visible) {
		// Nothing to do here. We don't really care that the itemContainer has
		// changed visibility...
	}
}